package cn.lupin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lupin.pojo.Bgm;
import cn.lupin.service.BgmService;
import cn.lupin.utils.RabbitMqSender;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.lupin.enums.BGMOperatorTypeEnum;
import cn.lupin.mapper.BgmMapper;
import cn.lupin.pojo.BgmExample;
import cn.lupin.utils.PagedResult;

@Service
public class BgmServiceImpl implements BgmService {

    @Autowired
    private RabbitMqSender rabbitMqSender;

    @Autowired
    private BgmMapper bgmMapper;

    @Autowired
    private Sid sid;

    @Override
    public PagedResult queryBgmList(Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);

        BgmExample example = new BgmExample();
        List<Bgm> list = bgmMapper.selectByExample(example);

        PageInfo<Bgm> pageList = new PageInfo<>(list);

        PagedResult result = new PagedResult();
        result.setTotal(pageList.getPages());
        result.setRows(list);
        result.setPage(page);
        result.setRecords(pageList.getTotal());

        return result;
    }

    @Override
    public void addBgm(Bgm bgm) {
        String bgmId = sid.nextShort();
        bgm.setId(bgmId);
        bgmMapper.insert(bgm);

        Map<String, String> map = new HashMap<>();
        map.put("operateType", BGMOperatorTypeEnum.ADD.type);
        String fileSpace = "/root/app/tomcat";
        map.put("path", fileSpace + bgm.getPath());
        map.put("bgmId", bgm.getId());

        rabbitMqSender.sendMessage(map);
    }

    @Override
    public void deleteBgm(String id) {
        Bgm bgm = bgmMapper.selectByPrimaryKey(id);
        bgmMapper.deleteByPrimaryKey(bgm.getId());
    }
}
